========

SETUP

========

FILEPATH:

filepath = "https://raw.githubusercontent.com/shabanm2/Utqiagvik/main/Analysis_Ready_Data/"

PICK DATE RANGES:

years = c("2022", "2023")
seasons = c("Spring", "Summer","Fall")
sites = c("TNHA", "BEO", "SSMH")

# put season values for the season that has the start of the data
date_start = "2022-06-01" # data starts in June 2022 (YEAR-MO-DY) where day is always 01
# put season values for the season that has the last of the data
date_end = "2023-11-01" # data ends after November of 2023 (will get data up UNTIL date_end not after)

PICK OUTPUT:

scree = F # scree plot
eigen = T # eigenvectors and eigenvalues

Dates (for selecting date ranges; no need to edit)

spring_months = c("March", "April", "May")
summer_months = c("June","July","August")
fall_months = c("September", "October", "November")
winter_months = c("December", "January", "February")
spring_dates = data.frame(months=spring_months, start=c("-03-01","-04-01","-05-01"), end=c("-04-01","-05-01","-06-01"))

summer_dates = data.frame(months=summer_months, start=c("-06-01","-07-01","-08-01"), end=c("-07-01","-08-01","-09-01"))

fall_dates = data.frame(months=fall_months, start=c("-09-01","-10-01","-11-01"), end=c("-10-01","-11-01","-12-01"))

winter_dates = data.frame(months=winter_months, start=c("-12-01","-01-01","-02-01"), end=c("-01-01","-02-01","-03-01"))
all_dates = data.frame(matrix(nrow = 0, ncol = 4))
for(yur in years){
  # spring
  if("Spring" %in% seasons){
    for(i in c(1:3)){
      curdate = as.Date(paste0(yur, spring_dates[i, 2]))
      if(curdate >= date_start && curdate < date_end){
        all_dates <- rbind(all_dates, c(spring_dates[i,1], paste0(yur, spring_dates[i, 2]), paste0(yur, spring_dates[i, 3]),"Spring"))
      }
    }
  }
  if("Summer" %in% seasons){
    for(i in c(1:3)){
      curdate = as.Date(paste0(yur, summer_dates[i, 2]))
      if(curdate >= date_start && curdate < date_end){
        all_dates <- rbind(all_dates, c(summer_dates[i,1], paste0(yur, summer_dates[i, 2]), paste0(yur, summer_dates[i, 3]),"Summer"))
      }
    }
  }
  if("Fall" %in% seasons){
    for(i in c(1:3)){
      curdate = as.Date(paste0(yur, fall_dates[i, 2]))
      if(curdate >= date_start && curdate < date_end){
        all_dates <- rbind(all_dates, c(fall_dates[i,1], paste0(yur, fall_dates[i, 2]), paste0(yur, fall_dates[i, 3]),"Fall"))
      }
    }
  }
  if("Winter" %in% seasons){
    for(i in c(1:3)){
      curdate = as.Date(paste0(yur, winter_dates[i, 2]))
      if(curdate >= date_start && curdate < date_end){
        all_dates <- rbind(all_dates, c(winter_dates[i,1], paste0(yur, winter_dates[i, 2]), paste0(yur, winter_dates[i, 3]),"Winter"))
      }
    }
  }
  
}
colnames(all_dates) = c("months","start","end","szn")

Packages

library(dplyr)
library(lubridate)
library(tidyverse)

Load all data

daily <- read.csv(paste0(filepath, "daily_2022_2024.csv"))
daily <- daily %>% select(-X) %>% select(-X.1) # get rid of index columns
daily$Date <- as.POSIXct(daily$date, format="%Y-%m-%d") # format dates
daily$fullname[daily$site == "BEO"] <- "BEO-BASE"
daily <- daily %>% filter(fullname == "TNHA-SA" | fullname == "TNHA-SC" | fullname == "SSMH-SB" | fullname == "SSMH-SA" | fullname == "BEO-BASE") %>% select(-c(winddir, date)) %>% mutate(aspect = case_when(fullname == "TNHA-SC" | fullname == "SSMH-SB" ~ "North", fullname == "TNHA-SA" | fullname == "SSMH-SA" ~ "South", .default = "N/A")) %>% filter(grounddepth == 8) %>% filter(Date >= "2022-06-19") # create "aspect" column and filter for top depth of soil and start date of when we started collecting data
# note: the data before June 19, 2022 was estimated by our gap-filling script and should be disregarded due to extrapolation

=========================

DEFINE FUNCTIONS

=========================

Temporal Range: Season Vertical Spatial Range: 30-45 cm Horizontal Spatial Range: stations across site (TNHA, SSMH, BEO) –> Average Total Site –> North vs South (except for BEO)

Filter by Site and Join Tables

pick_site <- function(cursite){

  big_df <<- daily %>% filter(site == cursite)
  
  if(szn == "Winter"){
    big_df <<- big_df %>% select(-solar)
  }
  return(big_df)
}

Filter by Date Range

pick_dates <- function(datemin, datemax, big_df){
  pca_df <<- big_df %>% filter(Date >= datemin) %>% filter(Date < datemax)
  
  # get rid of NAs
  pca_df <<- na.omit(pca_df)
  pca_df <<- unique(pca_df)
  return(pca_df)
}

Calculate PCA

calc_pca <- function(pca_df){
  pca <<- prcomp(pca_df[,6:(ncol(pca_df)-2)], center=TRUE, scale.=TRUE)

  #take out variables
  sd <- pca$sdev
  loads <<- pca$rotation
  rownames(loads) <<- colnames(pca_df[6:(ncol(pca_df)-2)])
  scores <<- pca$x
  
  var <- sd^2
  varPercent <- var/sum(var) * 100
  
  return(list("pca"=pca, "loads"=loads))
}

Make Scree Plot

make_scree <- function(pca){
  sd <- pca$sdev
  
  var <- sd^2
  varPercent <- var/sum(var) * 100
  
  barplot(varPercent, xlab="PC", ylab="Percent Variance", names.arg=1:length(varPercent), 
          las=1, ylim=c(0, max(varPercent)), col="gray")
  abline(h=1/ncol(pca_df[5:ncol(pca_df)])*100, col="red")
}

Display Eigenvectors and Eigenvalues

make_eigen <- function(pca){
  eigenvectors <- pca$rotation
  print("Eigenvectors (Loadings):")
  print(eigenvectors)
  
  print("Loadings Cutoff:")
  sqrt(1/ncol(pca_df[5:ncol(pca_df)])) # cutoff for "important" loadings
  
  # Access the eigenvalues (variances of the principal components)
  eigenvalues <- (pca$sdev)^2
  print("Eigenvalues:")
  print(eigenvalues)
}

===============

PCA PLOTS

===============

cursite = "BEO"
make_pca <- function(pca_df, szn, yr, cursite){
  if (cursite == "BEO")
  {
    SOUTH <<- pca_df$site == cursite
    # n <- "BEO"
  } else {
    SOUTH <<- pca_df$site == cursite & pca_df$aspect == "South"
    NORTH <<- pca_df$site == cursite & pca_df$aspect == "North"
    s <- pca_df$fullname[SOUTH][1]
    n <- pca_df$fullname[NORTH][1]
  }

    scaling <- 2
  textNudge <- 1.1
  limNudge <- 1.3
  
  xlimit <- seq(floor(min(scores[,1])*limNudge),ceiling(max(scores[,1])*limNudge), 1)
  ylimit <- seq(floor(min(scores[,2])*limNudge),ceiling(max(scores[,2])*limNudge), 1)
  
  plot(scores[, 1], scores[, 2], xlab="PCA 1", ylab="PCA 2", type="n", asp=1, 
       las=1, xaxt='n', yaxt='n')
  
  axis(side = 1, at=xlimit)
  axis(side = 2, at=ylimit)

  if (cursite == "BEO")
  {
    nvstext <- " "
  } else {
    nvstext <- " North v. South "
  }
  
  mindate = format(as.Date(min(pca_df$Date)), format="%B %d %Y")
  maxdate = format(as.Date(max(pca_df$Date)), format="%B %d %Y")
  
  title(paste0(szn, " ", yr," Principal Component Analysis:\n", site, nvstext, "\n(", mindate," - ", maxdate, ")"), adj=0.5)
  

  points(scores[SOUTH, 1], scores[SOUTH, 2], pch=16, cex=1, col="mediumturquoise")
   
  if(cursite != "BEO"){
    points(scores[NORTH, 1], scores[NORTH, 2], pch=16, cex=1, col="salmon")
     legend(x = "topright",          # Position
       legend = c(paste0(s, " (south)"), paste0(n, " (north)")),  # Legend texts
       col = c("mediumturquoise","salmon"),
       pch = 19)  #colors
  
  } else{
    legend(x = "topright",          # Position
       legend = "BEO",  # Legend texts
       col = "mediumturquoise",
       pch = 19) 
    
  }
  
   
  
  arrows(0, 0, loads[, 1]* scaling, loads[, 2]* scaling, length=0.1, angle=20, col="darkred", lwd=1.4)
   
  text(loads[1, 1]*scaling*(textNudge+0.4), loads[1, 2]*scaling*textNudge, rownames(loads)[1],   col="darkred", cex=1) # ground label
  
  text(loads[2, 1]*scaling*textNudge, loads[2, 2]*scaling*textNudge, rownames(loads)[2],   col="darkred", cex=1) # vwc label
  
  if(nrow(loads) > 2){
    text(loads[3, 1]*scaling*(textNudge+0.2), loads[3, 2]*scaling*textNudge, rownames(loads)[3],   col="darkred", cex=1) # airtemp label
  
    if(nrow(loads)>3){
      text(loads[4, 1]*scaling*textNudge-0.2, loads[4, 2]*scaling*textNudge, rownames(loads)[4],   col="darkred", cex=1) # solar or wind label
      
      if(nrow(loads)>4){
  
  text(loads[5, 1]*scaling*textNudge, loads[5, 2]*scaling*textNudge, rownames(loads)[5],   col="darkred", cex=1) # solar or wind label
    }

    }
  
  
  }
  
  
 
  #text(-3, 1]*scaling*textNudge, 1, "TNHA-SA \n(south)", col="mediumturquoise")
  #text(1, 1, "TNHA-SC \n(north)", col="salmon")
}
for(i in c(1:nrow(all_dates))){
  
  month <- all_dates$months[i]
  startdate <- all_dates$start[i]
  enddate <- all_dates$end[i]
  szn <<- all_dates$szn[i]
  yr <<- substr(all_dates$start[i], 1, 4)
  
  for(site in sites){
    big_df <- pick_site(site)
    pca_df <- pick_dates(startdate, enddate, big_df)
    
    if(nrow(pca_df) > 4){
      p <- calc_pca(pca_df)
      pca <- p$pca
      loads <- p$loads
      if(scree == T){
        make_scree(pca)
      }
      if(eigen == T){
        make_eigen(pca)
      }
      make_pca(pca_df, szn, yr, site)
              
    }
  }
  
}
## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2        PC3        PC4         PC5
## groundtemp  0.4251122 -0.5364598 -0.2407295 -0.4133944 -0.55013163
## vwc         0.4610666  0.4455959 -0.1324337  0.5943873 -0.46693353
## airtemp     0.2457283 -0.6684857  0.2329728  0.5933527  0.29394004
## solar       0.4733689  0.2029974  0.7998823 -0.3045098  0.04664754
## windspeed  -0.5676589 -0.1599190  0.4800235  0.1761114 -0.62510122
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.50736756 1.48726456 0.52665081 0.42923869 0.04947839

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2         PC3         PC4         PC5
## groundtemp  0.6253691 -0.03642962  0.26834573  0.02681131  0.73133993
## vwc        -0.4149103  0.11681552  0.88724687  0.16170376  0.02912901
## airtemp     0.5863511  0.01557190  0.36115271 -0.37678473 -0.61931567
## solar      -0.1513777  0.74189147 -0.06343748 -0.61451378  0.21220349
## windspeed   0.2646510  0.65907631 -0.07954858  0.67345314 -0.18897405
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.2046058 1.2929138 0.7455709 0.6036530 0.1532565

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2        PC3        PC4          PC5
## groundtemp -0.5598058  0.1529916  0.1573013 -0.1082290  0.791677881
## vwc        -0.2062583 -0.7161809 -0.3693654 -0.5549893 -0.009927256
## airtemp    -0.4583888  0.4421088  0.2060784 -0.5279610 -0.522693317
## solar      -0.5206088  0.1204776 -0.6653414  0.4842728 -0.193008408
## windspeed   0.4036349  0.5036898 -0.5947084 -0.4086690  0.250373786
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.91688606 1.48745098 0.29334017 0.23682552 0.06549727

## [1] "Eigenvectors (Loadings):"
##                   PC1          PC2        PC3        PC4        PC5
## groundtemp  0.5005885  0.139278597  0.5411127 -0.3514919 -0.5600563
## vwc        -0.4850438 -0.001575782 -0.3453066 -0.7454221 -0.2997320
## airtemp     0.5019415  0.250012536 -0.1913861 -0.4942587  0.6361039
## solar       0.4873282 -0.094587831 -0.7354245  0.1923294 -0.4191952
## windspeed  -0.1571927  0.953491407 -0.1023846  0.1987890 -0.1270627
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.8827909 0.9931040 0.4672808 0.4444790 0.2123453

## [1] "Eigenvectors (Loadings):"
##                    PC1         PC2          PC3         PC4         PC5
## groundtemp -0.58667631 -0.01898579  0.025271565 -0.42273293 -0.69000628
## vwc        -0.09527540  0.69604273 -0.693875273  0.14838393 -0.05446499
## airtemp    -0.58857854  0.05130022 -0.001779096 -0.36226723  0.72090421
## solar      -0.54773225 -0.13369021  0.117921071  0.81700124 -0.02683095
## windspeed  -0.01726811 -0.70332018 -0.709922467 -0.02346297  0.02240791
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.5561720 1.2945940 0.7115630 0.2996129 0.1380581

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2        PC3        PC4          PC5
## groundtemp  0.5259826 -0.4005413  0.1483286 -0.2167816  0.702789664
## vwc        -0.3828728 -0.4944072 -0.4334503 -0.6409362 -0.101447327
## airtemp     0.4588767 -0.5612490  0.1647349  0.1505552 -0.651634318
## solar       0.4538942  0.1737036 -0.8562611  0.1748928 -0.006037956
## windspeed  -0.4002036 -0.4999548 -0.1726230  0.6992512  0.266704682
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.67319392 1.22900666 0.54132139 0.50007955 0.05639848

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3        PC4         PC5
## groundtemp  0.5530877 -0.3336903 -0.18911751  0.1519020 -0.72381293
## vwc        -0.3974952  0.3975353 -0.74053134 -0.1664253 -0.32844981
## airtemp     0.5118750 -0.1575360 -0.58942460 -0.1381457  0.58877907
## solar       0.4075547  0.4858120  0.26103558 -0.7159510 -0.13099805
## windspeed  -0.3285952 -0.6854091 -0.01693995 -0.6461875 -0.06628937
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.62723504 1.07204492 0.71710339 0.50808664 0.07553001

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2          PC3         PC4         PC5
## groundtemp  0.5678348 -0.1783765  0.312317167 0.009269662  0.74034958
## vwc        -0.4791918 -0.3001445  0.183268956 0.776764304  0.20817824
## airtemp     0.5360326 -0.1709436  0.414555647 0.336039631 -0.63140187
## solar       0.3750804 -0.2069657 -0.834816099 0.345624078  0.01029523
## windspeed   0.1411475  0.8977965  0.009806847 0.405182245  0.09884333
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.65125935 1.08469995 0.75768409 0.43943558 0.06692103

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2         PC3        PC4        PC5
## groundtemp -0.5359818 -0.20775100 -0.18249509 -0.2695806  0.7507229
## vwc        -0.4247851 -0.03177426  0.78857868  0.4418432  0.0382909
## airtemp    -0.4738421 -0.50535724 -0.03997266 -0.3668533 -0.6196035
## solar      -0.4579210  0.24919387 -0.56978368  0.6099759 -0.1774451
## windspeed   0.3131503 -0.79896583 -0.13633761  0.4748146  0.1398343
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 3.08533066 0.99556860 0.64310440 0.21649095 0.05950539

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2        PC3        PC4          PC5
## groundtemp -0.5617404 -0.18397305 -0.1968255 -0.3233802 -0.712240504
## vwc         0.3004320  0.68480190 -0.6261589 -0.1136529 -0.189195552
## airtemp    -0.5497470  0.02273978 -0.3868633 -0.3062375  0.673659117
## solar      -0.4538308  0.22107871 -0.1142437  0.8538471 -0.055274355
## windspeed   0.2932647 -0.66918411 -0.6375499  0.2442783  0.006829898
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.69774641 1.00238464 0.70734668 0.52938179 0.06314047

## [1] "Eigenvectors (Loadings):"
##                    PC1         PC2        PC3        PC4         PC5
## groundtemp -0.66112905  0.07464934 -0.1119890 -0.1810824 -0.71554419
## vwc         0.01737212  0.69064945 -0.2023746  0.6886552 -0.08660298
## airtemp    -0.60893173  0.26787199 -0.2056918 -0.2281556  0.68050178
## solar      -0.43574681 -0.40663491  0.5290211  0.5903631  0.12798784
## windspeed  -0.04397099 -0.52945582 -0.7901459  0.3039402  0.03213836
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.1000865 1.7578911 0.7655884 0.2714104 0.1050236

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2        PC3        PC4        PC5
## groundtemp -0.3940883  0.53987529  0.2453154 -0.6008097 -0.3634243
## vwc        -0.5197378 -0.33120476  0.0690995 -0.3524287  0.7008532
## airtemp    -0.4231970  0.44157204 -0.7186943  0.3079912  0.1205753
## solar      -0.5431087 -0.04902231  0.5313238  0.6275691 -0.1627322
## windspeed   0.3170240  0.63360005  0.3690789  0.1616175  0.5794026
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.89572705 1.62447480 0.29011136 0.14979481 0.03989199

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2         PC3        PC4         PC5
## groundtemp -0.5757028  0.25135167  0.05851454  0.2640940  0.72953344
## vwc        -0.4132803  0.03502202  0.82224829 -0.2106012 -0.32791424
## airtemp    -0.5314942  0.20481505 -0.41498768  0.3848498 -0.59602064
## solar      -0.4496213 -0.37507497 -0.36856224 -0.7191559  0.06431303
## windspeed   0.1145442  0.86773576 -0.11149393 -0.4696881 -0.02960466
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.66856794 1.15997724 0.77436873 0.37132121 0.02576488

## [1] "Eigenvectors (Loadings):"
##                    PC1         PC2         PC3        PC4         PC5
## groundtemp -0.47333707  0.26955930  0.55962083 -0.5952464  0.18919848
## vwc         0.50862839  0.23867852 -0.03655952 -0.5318118 -0.63258937
## airtemp    -0.05009781  0.92850225 -0.22345387  0.2784739  0.08885076
## solar      -0.53169595 -0.01856235  0.18945376  0.3558380 -0.74460827
## windspeed  -0.48170407 -0.08893456 -0.77437948 -0.3983586 -0.04121533
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 3.39268571 1.11599684 0.28945431 0.19122341 0.01063973

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3         PC4         PC5
## groundtemp  0.4468994 -0.5489422  0.10834328 -0.65999530  0.22718130
## vwc        -0.5658041 -0.4144006 -0.03849404 -0.27128658 -0.65807266
## airtemp     0.2462120 -0.6606640 -0.26672428  0.65516893 -0.05014557
## solar      -0.1327892 -0.1887350  0.93797278  0.24727348  0.07621687
## windspeed   0.6339506  0.2341725  0.18932817  0.02047668 -0.71204315
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.08823187 1.72861011 1.01689076 0.10814920 0.05811807

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3        PC4         PC5
## groundtemp -0.5315852 -0.3427027  0.02204563 -0.2538429 -0.73147097
## vwc        -0.5023974  0.1458198  0.78447293  0.2304738  0.24045280
## airtemp    -0.5176662 -0.3215083 -0.37051491 -0.3164480  0.62548661
## solar      -0.4271435  0.4307294 -0.49656848  0.6095449 -0.11787857
## windspeed   0.1207649 -0.7565686  0.01595833  0.6409067  0.04476392
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.83154392 1.44065358 0.38403261 0.32609385 0.01767604

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2          PC3         PC4         PC5
## groundtemp -0.3313202  0.50805224 -0.791439329 -0.05344011 -0.05364518
## vwc         0.5323690  0.29725406 -0.057364863 -0.34099677  0.71319968
## airtemp    -0.1612886  0.77510607  0.549302285  0.26545739 -0.03155868
## solar      -0.5444014 -0.22878515  0.006321892  0.40701695  0.69683691
## windspeed  -0.5333135 -0.01977055  0.261839826 -0.80295388  0.04348316
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 3.074722307 1.241479493 0.545416655 0.135859637 0.002521908

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3         PC4         PC5
## groundtemp  0.5316556  0.1398281 -0.21113470 -0.71216619  0.38214112
## vwc         0.5400300  0.1266697 -0.16951986  0.70096676  0.41500727
## airtemp     0.1790454 -0.8304039  0.46029892 -0.02830971  0.25631152
## solar       0.6062849 -0.1257532 -0.02486046  0.01421900 -0.78471942
## windspeed  -0.1614662 -0.5089385 -0.84509882  0.02147771 -0.01602987
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.5993254 1.0394969 0.9280314 0.3885757 0.0445706

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2         PC3        PC4         PC5
## groundtemp  0.5635074 -0.02576923 -0.04986423  0.3708856  0.73603856
## vwc        -0.4969384 -0.30834942  0.34094468  0.7356926  0.02204466
## airtemp     0.5585201  0.07309266 -0.09781410  0.4733103 -0.67016605
## solar       0.3268446 -0.18087547  0.88693548 -0.2641734 -0.06336071
## windspeed   0.1293815 -0.93069765 -0.29163015 -0.1654993 -0.06800106
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.8792237 1.0119011 0.8216424 0.1796022 0.1076306

## [1] "Eigenvectors (Loadings):"
##                    PC1          PC2         PC3          PC4         PC5
## groundtemp -0.44805192 -0.497693477  0.26630796  0.431996688 -0.54222654
## vwc         0.57059629 -0.036520834 -0.27758853  0.770995822  0.03995199
## airtemp     0.03792776 -0.804100074 -0.06864628 -0.120762316  0.57679174
## solar       0.53282596 -0.323093168 -0.20482395 -0.452057799 -0.60448176
## windspeed  -0.43396698 -0.001143578 -0.89741825  0.002124662 -0.07941863
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.5686994 1.4570252 0.6398661 0.1887535 0.1456559

## [1] "Eigenvectors (Loadings):"
##                    PC1         PC2         PC3          PC4          PC5
## groundtemp -0.64041141  0.06121735 -0.08294995  0.301876521  0.698652657
## vwc        -0.06190763 -0.79745617  0.59373492 -0.008688864  0.087375388
## airtemp    -0.64785804 -0.03906993 -0.01155910  0.273287143 -0.709883109
## solar      -0.16449828  0.57737895  0.75393437 -0.266730856  0.003387123
## windspeed  -0.37317684 -0.15944651 -0.26841665 -0.873478023  0.017450083
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0914148 1.0284922 0.9558375 0.8043603 0.1198953

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2        PC3        PC4         PC5
## groundtemp 0.55969266 -0.02662073 -0.1332922 -0.2487955 -0.77869728
## vwc        0.53203071  0.14782154 -0.2485944  0.7770703  0.17162344
## airtemp    0.55205517 -0.06122663 -0.1602147 -0.5505857  0.60222340
## solar      0.30833190 -0.28719696  0.8977859  0.1221922  0.03871561
## windspeed  0.06208038  0.94403884  0.2979022 -0.1272280  0.00200413
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 3.03913251 1.02974029 0.77465831 0.11276470 0.04370419

## [1] "Eigenvectors (Loadings):"
##                   PC1          PC2         PC3        PC4          PC5
## groundtemp 0.59730590 -0.002222981  0.03435141 -0.4322726 -0.674671108
## vwc        0.08974949  0.761135020 -0.57751613  0.2559856 -0.116468831
## airtemp    0.60810489  0.111308018 -0.03715198 -0.2973686  0.726643374
## solar      0.47531356 -0.016390585  0.40812880  0.7772168 -0.056332186
## windspeed  0.19863575 -0.638756928 -0.70522742  0.2347716 -0.008366521
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.43686045 1.17217854 0.79773023 0.52319263 0.07003815

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2         PC3         PC4        PC5
## groundtemp  0.5274417  0.20165485  0.07028542  0.61834264 -0.5420820
## vwc         0.5184045  0.08954906  0.26737800 -0.75421498 -0.2879350
## airtemp     0.5041727  0.40974468 -0.02318055  0.09937421  0.7533303
## solar       0.2581347 -0.76500358  0.51090425  0.17777203  0.2356055
## windspeed  -0.3636459  0.44519109  0.81363903  0.08563881  0.0149682
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.99688985 1.08499060 0.58439978 0.23965995 0.09405981

## [1] "Eigenvectors (Loadings):"
##                    PC1         PC2        PC3         PC4        PC5
## groundtemp  0.49546733 -0.24167430  0.4188558 -0.59152719 -0.4132324
## vwc         0.06986936  0.78975205 -0.2728841 -0.53382203  0.1094453
## airtemp     0.62199813 -0.03553462  0.1479180  0.10910567  0.7603104
## solar      -0.46229988 -0.45911938 -0.1009098 -0.59353153  0.4615471
## windspeed  -0.38604086  0.32531819  0.8473674  0.03075549  0.1617501
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.9833278 1.1664056 0.7952641 0.6995336 0.3554689

## [1] "Eigenvectors (Loadings):"
##                    PC1          PC2        PC3         PC4        PC5
## groundtemp -0.53588719 -0.264868566  0.3092503  0.05903752 -0.7372573
## vwc        -0.09598454 -0.893339105 -0.2986597 -0.20364180  0.2491277
## airtemp    -0.52787283 -0.007976136  0.4850361  0.32620061  0.6161330
## solar      -0.44394747  0.189538837 -0.7512130  0.44951510 -0.0245121
## windspeed   0.47736025 -0.309517847  0.1248424  0.80409821 -0.1190224
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.9972007 1.1409249 0.5423637 0.3079312 0.0115795

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2          PC3       PC4        PC5
## groundtemp  0.2272370  0.7948439  0.266742708 0.3317312 -0.3679528
## vwc         0.4427129 -0.5626464  0.004964568 0.4967322 -0.4905780
## airtemp     0.5701671  0.1088441 -0.226420350 0.3215691  0.7130145
## solar      -0.4225890  0.1456847 -0.776048212 0.4274196 -0.1235160
## windspeed  -0.4986951 -0.1363124  0.524696749 0.5975929  0.3166989
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.6348532 1.2067381 0.7439352 0.2956042 0.1188692

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3         PC4         PC5
## groundtemp  0.3425073 -0.6098518  0.18528174  0.19523423 -0.66206023
## vwc        -0.5881306 -0.1359353  0.30473100 -0.67591742 -0.29308477
## airtemp     0.4940881 -0.4375553  0.07775351 -0.53366387  0.52304837
## solar       0.4414546  0.4594809 -0.41509960 -0.46885154 -0.44929462
## windspeed   0.3127075  0.4550000  0.83334052  0.02000839 -0.01822926
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.8530637 1.6121629 0.6979920 0.5190370 0.3177444

## [1] "Eigenvectors (Loadings):"
##                    PC1        PC2        PC3       PC4         PC5
## groundtemp -0.64507107  0.1766376 -0.1289079 0.1536096  0.71586963
## vwc         0.40154708  0.4660722 -0.1004291 0.7795326  0.06147873
## airtemp    -0.59336705  0.3300193 -0.2265976 0.1332064 -0.68550154
## solar      -0.26295786 -0.5496827  0.5630472 0.5458719 -0.11706300
## windspeed   0.03757605 -0.5835341 -0.7777712 0.2302453 -0.01161591
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.1209270 1.4975648 0.7583133 0.5343873 0.0888076

## [1] "Eigenvectors (Loadings):"
##                    PC1          PC2        PC3         PC4         PC5
## groundtemp -0.62117176  0.073618755 -0.2615015  0.16117669  0.71719240
## vwc        -0.37313464 -0.487316795  0.2324940 -0.75424328 -0.01888065
## airtemp    -0.61616139 -0.003696849 -0.2926458  0.23434362 -0.69265646
## solar      -0.30714098  0.519683106  0.7936202  0.06191055 -0.04390927
## windspeed  -0.03042591 -0.697867052  0.4026027  0.58854847  0.05981285
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.37509340 1.50288767 0.58441164 0.49361584 0.04399145

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2        PC3        PC4         PC5
## groundtemp -0.4894196  0.24173580 -0.4400715 -0.6321460  0.32978895
## vwc        -0.1195654 -0.96763100 -0.1582017 -0.1539919  0.02555569
## airtemp    -0.5305440  0.05523545 -0.3524142  0.3101331 -0.70362904
## solar       0.4116833  0.02569158 -0.7913480  0.3738383  0.25272526
## windspeed  -0.5433346 -0.03928160  0.1757325  0.5837297  0.57586654
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.9074020 0.9980963 0.7473896 0.2158059 0.1313061

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3        PC4          PC5
## groundtemp -0.5714401  0.2912852 -0.08088859 -0.2489714  0.721165269
## vwc         0.3125049  0.8022959 -0.17382270  0.4731814  0.067431543
## airtemp    -0.5467124  0.3758039 -0.17110028 -0.2410166 -0.687395596
## solar      -0.4857921 -0.3194091 -0.21627336  0.7842965 -0.009413303
## windspeed  -0.2022584  0.1679985  0.94190936  0.2022436 -0.052652889
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.60690640 0.97670560 0.95764864 0.39072132 0.06801805

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3         PC4         PC5
## groundtemp -0.2615584 -0.6945194  0.60881200 -0.11522305  0.25554192
## vwc        -0.5783951  0.1390945  0.20928933 -0.12008096 -0.76674011
## airtemp    -0.5079515  0.1790256  0.01143968  0.78909083  0.29519462
## solar      -0.4964565  0.3717024 -0.11249834 -0.59072456  0.50374283
## windspeed   0.3042408  0.5727851  0.75680855  0.02616032  0.07688448
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.3867857 1.1519079 0.6972815 0.5239479 0.2400770

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2        PC3         PC4        PC5
## groundtemp -0.4775224  0.4820039 0.08879957 -0.69454464 -0.2221869
## vwc         0.1587978 -0.7681367 0.17802634 -0.58333663 -0.1130236
## airtemp    -0.5707893 -0.3060000 0.07881668  0.39672358 -0.6457259
## solar      -0.5610982 -0.1884892 0.40952549  0.08699419  0.6887390
## windspeed   0.3257667  0.2201685 0.88684726  0.11121394 -0.2157203
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0839193 1.3162225 0.8845549 0.4081203 0.3071830

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2        PC3         PC4        PC5
## groundtemp -0.5144647  0.0737107  0.2081860  0.82216815 -0.1029124
## vwc         0.4215249 -0.4224955  0.6523562  0.07881904 -0.4604707
## airtemp    -0.3550616 -0.7176219  0.1743267 -0.13216580  0.5577567
## solar      -0.5577223 -0.2381581 -0.2271200 -0.35455471 -0.6744879
## windspeed   0.3471599 -0.4943318 -0.6701617  0.41791177 -0.1065329
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0381112 1.1314075 0.8152753 0.6136902 0.4015157

If you want to just make one plot (for testing):

  # look at all_dates and pick a row to use (set to i)

i <- 8
site <- "BEO"

month <- all_dates$months[i]
startdate <- all_dates$start[i]
enddate <- all_dates$end[i]
szn <<- all_dates$szn[i]
yr <<- substr(all_dates$start[i], 1, 4)

big_df <- pick_site(site)
pca_df <- pick_dates(startdate, enddate, big_df)


if(nrow(pca_df > 0)){
    p <- calc_pca(pca_df)
    pca <- p$pca
    loads <- p$loads # not used at the moment
    if(scree == T){
      make_scree(pca)
    }
    if(eigen == T){
      make_eigen(pca)
    }
    make_pca(pca_df, szn, yr, site)
            
  }
## [1] "Eigenvectors (Loadings):"
##                    PC1         PC2         PC3          PC4          PC5
## groundtemp -0.64041141  0.06121735 -0.08294995  0.301876521  0.698652657
## vwc        -0.06190763 -0.79745617  0.59373492 -0.008688864  0.087375388
## airtemp    -0.64785804 -0.03906993 -0.01155910  0.273287143 -0.709883109
## solar      -0.16449828  0.57737895  0.75393437 -0.266730856  0.003387123
## windspeed  -0.37317684 -0.15944651 -0.26841665 -0.873478023  0.017450083
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0914148 1.0284922 0.9558375 0.8043603 0.1198953